<!--
 * @Descripttion: 
 * @version: 
 * @Author: ruxiang.wang
 * @Date: 2021-06-04 17:06:37
 * @LastEditors: ruxiang.wang
 * @LastEditTime: 2021-07-02 15:05:06
-->
<template>
  <div class="app-container">
    <!-- 搜索查询 -->
    <div class="list-search">
      <el-row :gutter="20">
        <el-col :span="6">
          <span class="list-search-tip">方案名称：</span>
          <el-input
            class="list-search-input"
            placeholder="请输入内容"
            v-model="listQuery.name">
          </el-input>
        </el-col>
        <el-col :span="6">
          <span class="list-search-tip">国家：</span>
          <el-select class="list-search-input" v-model="listQuery.region" :multiple="true" filterable placeholder="请选择">
            <el-option
              v-for="item in regionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <span class="list-search-tip">机型：</span>
          <el-select class="list-search-input" v-model="listQuery.model" :multiple="true" filterable placeholder="请选择">
            <el-option
              v-for="item in modelOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-search" @click="listSearch">搜索</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 新建 -->
    <div class="list-add">
      <el-button type="primary" icon="el-icon-document-add" @click="addData">新建</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="120">
      </el-table-column>
      <el-table-column
        prop="name"
        label="方案名称"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="region"
        label="生效国家"
        show-overflow-tooltip
        width="280">
      </el-table-column>
      <el-table-column
        prop="model"
        label="机型"
        show-overflow-tooltip
        width="280">
      </el-table-column>
      <el-table-column
        prop="state"
        label="发布状态"
        width="100">
        <template slot-scope="scope">
          <span
            v-if="scope.row.state =='0'"
            style="color:red;"
          >未发布</span>
          <span
            v-if="scope.row.state =='1'"
            style="color:green;"
          >已发布</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="operationTime"
        label="操作时间"
        width="180">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="180">
        <template slot-scope="scope">
          <el-button v-if="scope.row.state =='0'" type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="copy(scope.row)">复制</el-button>
          <el-button v-if="scope.row.state =='0'" type="text" size="small" @click="del(scope.row)">删除</el-button>
          <el-button v-if="scope.row.state =='0'" type="text" size="small" @click="release(scope.row)">发布</el-button>
          <el-button v-if="scope.row.state =='1'" type="text" size="small" @click="unpublish(scope.row)">取消发布</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging" v-if="tableData.length>0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="50%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="方案名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部署国家" prop="region">
          <el-select style="width:100%;" v-model="ruleForm.region" :multiple="true" placeholder="请选择部署国家">
            <el-option
              v-for="item in regionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效机型" prop="model">
          <el-button size="medium" type="primary" @click="innerVisible=true">添加机型</el-button>
          <el-button :disabled="tableDataModel.length==0" size="medium" type="primary" style="margin-left:20px;" @click="delModelAll(ruleForm)">清空机型</el-button>
        </el-form-item>
        <!-- 选择机型数据表格 -->
        <el-table
          v-if="tableDataModel.length>0"
          :data="tableDataModel"
          border
          style="width: 100%;margin-top:20px;">
          <el-table-column
            prop="nameInner"
            label="机型code"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="操作"
            width="180">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="delModel(scope.row)">删除机型</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
          width="45%"
          title="选择机型"
          :visible.sync="innerVisible"
          :close-on-click-modal="false"
          append-to-body>
          <!-- 选择机型搜索查询 -->
          <div class="list-search">
            <el-row :gutter="20">
              <el-col :span="9">
                <span class="list-search-tip">机型名称：</span>
                <el-input
                  class="list-search-input"
                  placeholder="请输入内容"
                  v-model="listQueryInner.nameInner">
                </el-input>
              </el-col>
              <el-col :span="9">
                <span class="list-search-tip">客户码：</span>
                <el-input
                  class="list-search-input"
                  placeholder="请输入内容"
                  v-model="listQueryInner.codeInner">
                </el-input>
              </el-col>
              <el-col :span="6">
                <el-button type="primary" icon="el-icon-search" @click="listSearchInner">搜索</el-button>
              </el-col>
            </el-row>
          </div>
          <!-- 选择机型数据表格 -->
          <el-table
            :data="tableDataInner"
            border
            style="width: 100%;margin-top:20px;"
            @selection-change="handleSelectionChange">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              align="center"
              label="序号"
              type="index"
              :index="indexMethodInner"
              width="50">
            </el-table-column>
            <el-table-column
              prop="nameInner"
              label="机型"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              prop="codeInner"
              label="客户码"
              show-overflow-tooltip>
            </el-table-column>
          </el-table>
          <!-- 选择机型数据分页 -->
          <div class="paging" v-if="tableData.length>0">
            <el-pagination
              @size-change="handleSizeChangeInner"
              @current-change="handleCurrentChangeInner"
              :current-page="currentPageInner"
              :page-sizes="[10, 20, 30, 40, 50]"
              :page-size="10"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalInner">
            </el-pagination>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="innerVisible=false">取 消</el-button>
            <el-button type="primary" @click="selectModel">确 定</el-button>
          </span>
        </el-dialog>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {

  data() {
    return {
      // 搜索条件
      listQuery:{
        name:'',
        region:'',
        model:''
      },
      // 国家下拉框数据
      regionOptions:[{
        value: 'chinese',
        label: '中国'
      }, {
        value: 'usa',
        label: '美国'
      }],
      // 机型下拉框数据
      modelOptions:[{
        value: 'TCL',
        label: 'TCL'
      }, {
        value: 'HUAWEI',
        label: 'HUAWEI'
      }],
      // 表格数据
      tableData: [{
        id: '20160502',
        name: '王小虎1',
        region: ['usa'],
        model: ['TCL'],
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎2',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '1',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎3',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      },
      {
        id: '20160502',
        name: '王小虎',
        region: '上海、上海市普陀区金沙江路 1518 弄',
        model: '普陀区、上海市普陀区金沙江路 1518 弄',
        state: '0',
        operationTime: '2021-07-01 14:47:58'
      }],
      // 分页
      currentPage: 1,
      total:100,
      // 第一层弹窗
      dialogTitle:'',
      dialogVisible: false,
      tableDataModel:[{
        nameInner: 'TCL',
        codeInner: '20160502',
      }],
      ruleForm: {
        name: '',
        region: '',
        model: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择部署国家', trigger: 'change' }
        ],
        model: [
          { required: true, message: '请选择生效机型', trigger: 'change' }
        ],
      },
      // 内层弹窗
      innerVisible:false,
      listQueryInner:{
        nameInner:'',
        codeInner:'',
      },
      tableDataInner:[{
        nameInner: 'TCL',
        codeInner: '20160502',
      },
      {
        nameInner: 'HUAWEI',
        codeInner: '20160503',
      }],
      multipleSelection: [],
      currentPageInner: 1,
      totalInner:100,
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree2.filter(val)
    }
  },

  created(){
    // debugger
  },

  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 表格序号
    indexMethodInner(index) {
      return index+1;
    },
    // 搜索
    listSearch(){
      console.log(this.listQuery.name+'==='+this.listQuery.region+'==='+this.listQuery.model);
    },
    // 选择机型弹窗搜索
    listSearchInner(){
      console.log(this.listQueryInner.nameInner+'==='+this.listQueryInner.codeInner);
    },
    // 选择机型弹窗表格多选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // debugger
    },
    // 删除机型
    delModel(row){
      this.$confirm('此操作将删除该机型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    // 清空机型
    delModelAll(ruleForm){
      this.$confirm('此操作将清空全部机型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '清空成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消清空'
        });          
      });
    },
    // 新建
    addData(){
      this.dialogTitle='新建';
      this.ruleForm={};
      this.tableDataModel=[];
      this.dialogVisible=true;
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    // 分页
    handleSizeChangeInner(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChangeInner(val) {
      console.log(`当前页: ${val}`);
    },
    // 复制
    copy(row) {
      this.$confirm('是否确定复制该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '复制成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消复制'
        });          
      });
    },
    // 编辑
    edit(row){
      const parameter={...row}
      this.dialogTitle='编辑';
      this.dialogVisible=true;
      this.ruleForm=parameter;
      // 需要设置tableDataModel
      // this.tableDataModel=parameter.tableDataModel
      console.log(row);
    },
    // 删除
    del(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    // 发布
    release(row){
      this.$confirm('是否确定发布该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '发布成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消发布'
        });          
      });
    },
    // 取消发布
    unpublish(row){
      this.$confirm('是否确定取消发布该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '取消发布成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消发布操作已终止'
        });          
      });
    },
    // 弹窗编辑保存
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
          console.log(this.ruleForm)
          this.dialogVisible=false;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible=false;
    },
    // 选择机型弹窗确认
    selectModel(){
      
    },
  }
}
</script>

<style lang="scss" scoped>
  .list-search-tip{
    width:30%;
    text-align: right;
  }
  .list-search-input{
    width: 70%;
  }
  .list-add{
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .paging{
    width: 100%;
    margin-top: 20px;
    text-align: right;
  }
</style>

